package com.googlecode.wheres_my_stuff_okay_potato;

import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.makeText;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

/**
 * Admin-only activity where a user can be deleted.
 */
public class DeleteUserActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delete_user);

		final LoginHandler lh = new LoginHandler(getApplication()
				.getBaseContext());

		final EditText usernameET = (EditText) findViewById(R.id.username_et);

		// Register button(should be delete)
		findViewById(R.id.register_b).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String username = usernameET.getText().toString();

				if (username.length() < 7) {
					makeText(getApplicationContext(),
							R.string.username_length_toast, LENGTH_LONG).show();
					usernameET.requestFocus();
					return;
				}

				// Deletes user
				if (lh.deleteUser(username)) {
					try {
						lh.saveDatabase();
						makeText(getApplicationContext(),
								R.string.account_deleted_toast, LENGTH_LONG)
								.show();
						finish();
					} catch (Exception e) {
						makeText(getApplicationContext(),
								R.string.database_save_failed, LENGTH_LONG)
								.show();
					}
				} else {
					makeText(getApplicationContext(),
							R.string.account_deleted_failed_toast, LENGTH_LONG)
							.show();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.delete_user, menu);
		return true;
	}

}
